#! /usr/bin/python
# coding=utf-8
import requests
import time


def getProxy():

    url = "http://api.tianqiip.com/getip?secret=fdjbfd2wcfwynwrl&num=1&type=json&port=1&time=3&mr=1&sign=4c0a11786d2299ec886e5153b559f65a"

    rsp = requests.get(url)

    if rsp.status_code != 200:
        raise Exception("get proxy failed, status code: " + str(rsp.status_code))

    rspJson = rsp.json()

    if rspJson["code"] != 1000:
        raise Exception("get proxy failed, code: " + str(rspJson["code"]))

    data = rspJson["data"][0]

    proxyMeta = f"http://{data['ip']}:{data['port']}"

    proxies = {"http": proxyMeta, "https": proxyMeta}

    return proxies


if __name__ == "__main__":
    targetUrl = "http://myip.ipip.net"
    start = int(round(time.time() * 1000))
    resp = requests.get(targetUrl, proxies=getProxy(), timeout=10)
    costTime = int(round(time.time() * 1000)) - start
    print(resp.text)
    print("耗时: " + str(costTime) + "ms")
